package edu.lingnan.librarymanage.controller;

import edu.lingnan.librarymanage.core.base.AjaxResult;
import edu.lingnan.librarymanage.core.constant.LibraryManageConstant;
import edu.lingnan.librarymanage.core.vo.LoginUserVO;
import edu.lingnan.librarymanage.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author cql
 */
@Controller
public class BookController {


    @Autowired
    private BorrowService borrowService;

    @ResponseBody
    @PostMapping("/borrow/{bookItemId}")
    public AjaxResult borrow(@PathVariable("bookItemId") Integer bookItemId, HttpSession session) {
        LoginUserVO user = (LoginUserVO) session.getAttribute(LibraryManageConstant.LOGIN_USER);
        if (user == null) {
            return AjaxResult.error("您还未登录!");
        }
        borrowService.borrow(bookItemId, user.getUserId());
        return AjaxResult.success();
    }

}
